package com.ruoyi.web.controller.order;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.order.domain.MesDetectNode;
import com.ruoyi.order.service.IMesDetectNodeService;
import com.ruoyi.order.service.IMesModelNodeRelaService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.MesProdModel;
import com.ruoyi.order.service.IMesProdModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产产品型号Controller
 * 
 * @author guoxl
 * @date 2025-06-01
 */
@RestController
@RequestMapping("/order/prodModel")
public class MesProdModelController extends BaseController
{
    @Autowired
    private IMesProdModelService mesProdModelService;

    @Autowired
    private IMesDetectNodeService stationService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IMesModelNodeRelaService mesModelNodeRelaService;

    /**
     * 查询生产产品型号列表
     */
    @PreAuthorize("@ss.hasPermi('order:prodModel:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesProdModel mesProdModel)
    {
        startPage();
        List<MesProdModel> list = mesProdModelService.selectMesProdModelList(mesProdModel);
        return getDataTable(list);
    }

    /**
     * 导出生产产品型号列表
     */
    @PreAuthorize("@ss.hasPermi('order:prodModel:export')")
    @Log(title = "生产产品型号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProdModel mesProdModel)
    {
        List<MesProdModel> list = mesProdModelService.selectMesProdModelList(mesProdModel);
        ExcelUtil<MesProdModel> util = new ExcelUtil<MesProdModel>(MesProdModel.class);
        util.exportExcel(response, list, "生产产品型号数据");
    }

    /**
     * 获取生产产品型号详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:prodModel:query')")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") Long modelId)
    {
        return success(mesProdModelService.selectMesProdModelByModelId(modelId));
    }

    /**
     * 新增生产产品型号
     */
    @PreAuthorize("@ss.hasPermi('order:prodModel:add')")
    @Log(title = "生产产品型号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProdModel mesProdModel)
    {
        return toAjax(mesProdModelService.insertMesProdModel(mesProdModel));
    }

    /**
     * 修改生产产品型号
     */
    @PreAuthorize("@ss.hasPermi('order:prodModel:edit')")
    @Log(title = "生产产品型号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProdModel mesProdModel)
    {
        return toAjax(mesProdModelService.updateMesProdModel(mesProdModel));
    }

    /**
     * 删除生产产品型号
     */
    @PreAuthorize("@ss.hasPermi('order:prodModel:remove')")
    @Log(title = "生产产品型号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(mesProdModelService.deleteMesProdModelByModelIds(modelIds));
    }

    @PreAuthorize("@ss.hasPermi('order:prodModel:query')")
    @GetMapping(value = "/stationTree/{modelId}")
    public AjaxResult stationTree(@PathVariable("modelId") Long modelId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", mesModelNodeRelaService.selectRelaListByModelId(modelId));
        SysDictData  dictData = new SysDictData();
        dictData.setDictType("subsys_code");
        List<SysDictData> dictDataList =  dictDataService.selectDictDataList(dictData);
        ajax.put("stations", stationService.selectStationTreeList(new MesDetectNode(),dictDataList));
        return ajax;
    }
}
